package com.mbuswidget;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MbusWidgetConfigurationSettingsBlacklistedRemoveRoute extends
		Activity {
	private ListView itemList;
	private Integer widgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
	private ArrayList<String> blacklistedRoutes;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectstopby);
		setResult(RESULT_CANCELED);
		
		widgetID = getIntent().getExtras().getInt(
				AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);

		if (widgetID == AppWidgetManager.INVALID_APPWIDGET_ID)
			finish();

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);

		itemList = (ListView) findViewById(R.id.itemList);
		
		blacklistedRoutes = MBusWidgetConstants.GetWidgetBlacklist(prefs, widgetID);
				
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, blacklistedRoutes);
		itemList.setAdapter(adapter);
				
		itemList.setOnItemClickListener(new OnItemClickListener() {
			// @Override
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {
				String[] routeToAdd = new String[1];
				routeToAdd[0] = blacklistedRoutes.get(position);
				
				Intent intent = new Intent(MbusWidgetConfigurationSettingsBlacklistedRemoveRoute.this, MbusWidgetConfigurationSettingsBlacklisted.class);
				intent.putExtra(MBusWidgetConstants.BLACKLISTED_ROUTE_REMOVE, routeToAdd);
				intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
				setResult(RESULT_OK, intent);
				finish();
			}
		});
		
	}
}
